import request from '@/utils/request'
import {
  SkuData,
  SpuData,
  AllTradeMark,
  HasSaleAttrResponseData,
  HasSpuResponseData,
  SaleAttrResponseData,
  SpuHasImg,
  SkuInfoData,
} from './type'

enum API {
  //获取已有的spu数据
  HASSPU_URL = '/admin/product/',
  //获取全部品牌数据
  ALLTRADEMARK_URL = '/admin/product/baseTrademark/getTrademarkList',
  //获取某一个spu下的全部的图片数据
  IMAGE_URL = '/admin/product/spuImageList/',
  //获取某一个spu下全部已有的销售属性地址
  SPUHASSALEATTR_URL = '/admin/product/spuSaleAttrList/',
  //获取整个项目全部的销售属性
  ALLSALEATTR_URL = '/admin/product/baseSaleAttrList',
  //追加一个新的spu
  ADDSPU_URL = '/admin/product/saveSpuInfo',
  //更新已有的spu
  UPDATE_URL = '/admin/product/updateSpuInfo',
  //追加一个新的sku地址
  ADDSKU_URL = '/admin/product/saveSkuInfo',
  //查看某一个已有的spu下全部的售卖商品
  SKUINFO_URL = '/admin/product/findBySpuId/',
  //删除已有spu
  REMOVESPU_URL = '/admin/product/deleteSpu/',
}

//获取3级分类下已有的spu数据
export const reqHasSpu = (
  page: number,
  limit: number,
  category3Id: number | string,
) =>
  request.get<any, HasSpuResponseData>(
    API.HASSPU_URL + `${page}/${limit}?category3Id=${category3Id}`,
  )

//获取全部的spu品牌数据
export const reqAllTradeMark = () =>
  request.get<any, AllTradeMark>(API.ALLTRADEMARK_URL)

//获取某一个已有的spu下全部的图片地址
export const reqSpuImageList = (spuid: number) =>
  request.get<any, SpuHasImg>(API.IMAGE_URL + spuid)

//获取某一个已有的spu拥有多少销售属性
export const reqSpuHasSaleAttr = (spuid: number) =>
  request.get<any, SaleAttrResponseData>(API.SPUHASSALEATTR_URL + spuid)

//获取全部销售属性
export const reqAllSaleAttr = () =>
  request.get<any, HasSaleAttrResponseData>(API.ALLSALEATTR_URL)

//添加一个新的spu接口
//更新已有的spu接口

export const reqAddOrUpdateSpu = (data: SpuData) => {
  //如果spu有id 则更新已有的spu
  if (data.id) {
    return request.post<any, any>(API.UPDATE_URL, data)
  } else {
    return request.post<any, any>(API.ADDSPU_URL, data)
  }
}

//添加sku的请求方法
export const reqAddSku = (data: SkuData) =>
  request.post<any, any>(API.ADDSKU_URL, data)

//获取sku数据
export const reqSkuList = (spuId: number | string) =>
  request.get<any, SkuInfoData>(API.SKUINFO_URL + spuId)

//删除已有的spu
export const reqRemoveSpu = (spuId: number | string) =>
  request.delete<any, any>(API.REMOVESPU_URL + spuId)
